package org.fujay.langchain4jserver.domain.knowledge.entity;

import lombok.*;
import org.fujay.langchain4jserver.domain.knowledge.vo.FileId;
import org.fujay.langchain4jserver.domain.knowledge.vo.IndexStatus;
import org.fujay.langchain4jserver.domain.knowledge.vo.KnowledgeBaseId;
import org.fujay.langchain4jserver.domain.knowledge.vo.StorageStrategy;

import java.time.LocalDateTime;

/**
 * 知识库文件实体
 */
@Getter
@Setter(AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class File {

    @NonNull
    private final FileId id;

    @NonNull
    private final KnowledgeBaseId knowledgeBaseId;

    @NonNull
    private String fileName;

    @NonNull
    private String filePath; // 实际存储路径或URL

    private Long fileSize;

    private String fileType;

    @NonNull
    private StorageStrategy storageStrategy;

    private LocalDateTime uploadTime;

    @NonNull
    private IndexStatus indexStatus;

    private String documentId;

    private String errorMessage;

    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // 工厂方法
    public static File create(
            FileId id,
            @NonNull KnowledgeBaseId knowledgeBaseId,
            String fileName,
            String filePath,
            Long fileSize,
            String fileType,
            StorageStrategy storageStrategy) {
        return new File(
                id,
                knowledgeBaseId,
                fileName,
                filePath,
                fileSize,
                fileType,
                storageStrategy,
                LocalDateTime.now(),
                IndexStatus.PENDING,
                null,
                null,
                LocalDateTime.now(),
                LocalDateTime.now());
    }

    // 加载方法 (供仓储使用)
    public static File load(
            FileId id,
            @NonNull KnowledgeBaseId knowledgeBaseId,
            String fileName,
            String filePath,
            Long fileSize,
            String fileType,
            StorageStrategy storageStrategy,
            LocalDateTime uploadTime,
            IndexStatus indexStatus,
            String errorMessage,
            LocalDateTime createTime,
            LocalDateTime updateTime) {
        return new File(
                id,
                knowledgeBaseId,
                fileName,
                filePath,
                fileSize,
                fileType,
                storageStrategy,
                uploadTime,
                indexStatus,
                null,
                errorMessage,
                createTime,
                updateTime);
    }

    // 更新索引状态
    public void updateIndexStatus(IndexStatus newStatus, String documentId, String errorMsg) {
        if (this.indexStatus == newStatus) {
            return;
        }
        this.indexStatus = newStatus;
        this.documentId = documentId;
        this.errorMessage = errorMsg;
        this.updateTime = LocalDateTime.now();
    }

    // 标记为待处理，用于重新索引
    public void markAsPending() {
        updateIndexStatus(IndexStatus.PENDING, null, null);
    }
}
